﻿Imports System.Data.SqlClient
Public Class frmTaiKhoan
    Private strMaTK As String = ""
    Public booThanhCong As Boolean = False
    Sub New(Optional ByVal MaTK As String = "")

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        strMaTK = MaTK
    End Sub

    Private Sub frmTaiKhoan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If strMaTK <> "" Then
            DocDuLieu(strMaTK)
        End If
        If strMaTK <> "" Then
            Me.txtMaTK.ReadOnly = True
        End If
    End Sub

    Private Sub btnLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuu.Click
        If validateInput() Then
            booThanhCong = CapNhatTK()
            If booThanhCong Then
                booThanhCong = CapNhat()
            End If
            If booThanhCong Then
                MsgBox("Lưu dữ liệu thành công!")
                strMaTK = Me.txtMaTK.Text
                Me.txtMaTK.ReadOnly = True
            Else
                MsgBox("Lưu dữ liệu thất bại!")
            End If
        End If

    End Sub

    Private Sub btnHuyBo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDong.Click
        Me.Close()
    End Sub

    Private Sub txtMaTK_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaTK.Enter
        txtMaTK.BackColor = System.Drawing.SystemColors.Window
    End Sub

    Private Sub txtMaTK_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaTK.Leave
        Me.txtMaTK.BackColor = System.Drawing.Color.FromArgb(CType(CType(255, Byte), Integer), CType(CType(255, Byte), Integer), CType(CType(192, Byte), Integer))
    End Sub

    Private Sub txtTenTK_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTenTK.Enter
        txtTenTK.BackColor = System.Drawing.SystemColors.Window
    End Sub

    Private Sub txtTenTK_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTenTK.Leave
        Me.txtTenTK.BackColor = System.Drawing.Color.FromArgb(CType(CType(255, Byte), Integer), CType(CType(255, Byte), Integer), CType(CType(192, Byte), Integer))
    End Sub

    Private Function CapNhat() As Boolean
        Dim strSql As String = ""
        Dim myConn As New SqlConnection(modKetNoi.strConnectionString)
        Dim myComm As New SqlCommand
        Dim result As Boolean = True
        Try
            myConn.Open()
            myComm.Connection = myConn
            If strMaTK <> "" Then
                strSql = "Update CTTaiKhoan set DuNo = @DuNo, DuCo = @DuCo, PhatSinhNo = @PhatSinhNo, PhatSinhCo = @PhatSinhCo, CuoiKyNo = @CuoiKyNo, CuoiKyCo = @CuoiKyCo where MaTK = @OriMaTK and Thang = @Thang and Nam = @Nam"
                myComm.Parameters.AddWithValue("@OriMaTK", strMaTK)
            Else
                strSql = "Insert into CTTaiKhoan(MaTK, Thang, Nam, DuNo, DuCo, PhatSinhNo, PhatSinhCo, CuoiKyNo, CuoiKyCo) values (@MaTK, @Thang, @Nam, @DuNo, @DuCo, @PhatSinhNo, @PhatSinhCo, @CuoiKyNo, @CuoiKyCo)"
                myComm.Parameters.AddWithValue("@MaTK", txtMaTK.Text.Trim())
            End If
            myComm.CommandText = strSql
            myComm.Parameters.AddWithValue("@Thang", modKetNoi.aryConfig("Thang"))
            myComm.Parameters.AddWithValue("@Nam", modKetNoi.aryConfig("Nam"))
            myComm.Parameters.AddWithValue("@DuNo", getEmptyValue(txtDuNo.Text))
            myComm.Parameters.AddWithValue("@DuCo", getEmptyValue(txtDuCo.Text))
            myComm.Parameters.AddWithValue("@PhatSinhNo", getEmptyValue(txtPSNo.Text))
            myComm.Parameters.AddWithValue("@PhatSinhCo", getEmptyValue(txtPSCo.Text))
            myComm.Parameters.AddWithValue("@CuoiKyNo", getEmptyValue(txtCKNo.Text))
            myComm.Parameters.AddWithValue("@CuoiKyCo", getEmptyValue(txtCKCo.Text))
            myComm.ExecuteNonQuery()
        Catch ex As Exception
            result = False
        End Try
        myConn.Close()
        myConn.Dispose()
        Return result
    End Function

    Private Function CapNhatTK() As Boolean
        Dim strSql As String = ""
        Dim myConn As New SqlConnection(modKetNoi.strConnectionString)
        Dim myComm As New SqlCommand
        Dim result As Boolean = True
        Try
            myConn.Open()
            myComm.Connection = myConn
            If checkExistTK(txtMaTK.Text.Trim()) Then
                strSql = "Update TaiKhoan set TenTK = @TenTK where MaTK = @OriMaTK"
                myComm.Parameters.AddWithValue("@OriMaTK", txtMaTK.Text.Trim())
            Else
                strSql = "Insert into TaiKhoan(MaTK, TenTK) values (@MaTK, @TenTK)"
                myComm.Parameters.AddWithValue("@MaTK", txtMaTK.Text.Trim())
            End If
            myComm.CommandText = strSql
            myComm.Parameters.AddWithValue("@TenTK", txtTenTK.Text.Trim())
            myComm.ExecuteNonQuery()
        Catch ex As Exception
            result = False
        End Try
        myConn.Close()
        myConn.Dispose()
        Return result
    End Function

    Private Sub DocDuLieu(ByVal MaTK As String)
        Dim strSql As String = "Select * from CTTaiKhoan ct inner join taikhoan tk on tk.matk = ct.matk where ct.MaTK = @MaTK and Thang = " & modKetNoi.aryConfig("Thang") & " and Nam = " & modKetNoi.aryConfig("Nam")
        Dim myConn As New SqlConnection(modKetNoi.strConnectionString)
        Dim myTable As New DataTable
        Try
            myConn.Open()
            Dim myAdapter As New SqlDataAdapter(strSql, myConn)
            myAdapter.SelectCommand.Parameters.AddWithValue("@MaTK", MaTK)
            myAdapter.Fill(myTable)
            If myTable.Rows.Count > 0 Then
                Me.txtMaTK.Text = myTable.Rows(0).Item("MaTK")
                Me.txtTenTK.Text = myTable.Rows(0).Item("TenTK")
                Me.txtDuNo.Text = myTable.Rows(0).Item("DuNo")
                Me.txtDuCo.Text = myTable.Rows(0).Item("DuCo")
                Me.txtPSNo.Text = myTable.Rows(0).Item("PhatSinhNo")
                Me.txtPSCo.Text = myTable.Rows(0).Item("PhatSinhCo")
                Me.txtCKNo.Text = myTable.Rows(0).Item("CuoiKyNo")
                Me.txtCKCo.Text = myTable.Rows(0).Item("CuoiKyCo")
            Else
                Me.txtMaTK.Text = MaTK
                strMaTK = ""
            End If
        Catch ex As Exception

        End Try
        myConn.Close()
        myConn.Dispose()
    End Sub

    Private Function validateInput() As Boolean
        If Me.txtMaTK.Text.Trim = "" Then
            MsgBox(modError.TK_ERR_MATK_REQUIRED, MsgBoxStyle.Information)
            txtMaTK.Focus()
            Return False
        Else
            If checkExist(Me.txtMaTK.Text.Trim, strMaTK) Then
                MsgBox(modError.TK_ERR_MATK_EXISTED, MsgBoxStyle.Information)
                txtMaTK.Focus()
                Return False
            End If
        End If

        If Me.txtTenTK.Text.Trim = "" Then
            MsgBox(modError.TK_ERR_TENTK_REQUIRED, MsgBoxStyle.Information)
            txtTenTK.Focus()
            Return False
        End If

        If Me.txtDuNo.Text.Trim <> "" AndAlso Not IsNumeric(Me.txtDuNo.Text) Then
            MsgBox(modError.TK_ERR_DUNO_INPUT_WRONG, MsgBoxStyle.Information)
            txtDuNo.Focus()
            Return False
        End If
        If Me.txtDuCo.Text.Trim <> "" AndAlso Not IsNumeric(Me.txtDuCo.Text) Then
            MsgBox(modError.TK_ERR_DUCO_INPUT_WRONG, MsgBoxStyle.Information)
            txtDuCo.Focus()
            Return False
        End If
        If Me.txtPSNo.Text.Trim <> "" AndAlso Not IsNumeric(Me.txtPSNo.Text) Then
            MsgBox(modError.TK_ERR_PSNO_INPUT_WRONG, MsgBoxStyle.Information)
            txtPSNo.Focus()
            Return False
        End If
        If Me.txtPSCo.Text.Trim <> "" AndAlso Not IsNumeric(Me.txtPSCo.Text) Then
            MsgBox(modError.TK_ERR_PSCO_INPUT_WRONG, MsgBoxStyle.Information)
            txtPSCo.Focus()
            Return False
        End If
        If Me.txtCKNo.Text.Trim <> "" AndAlso Not IsNumeric(Me.txtCKNo.Text) Then
            MsgBox(modError.TK_ERR_CKNO_INPUT_WRONG, MsgBoxStyle.Information)
            txtCKNo.Focus()
            Return False
        End If
        If Me.txtCKCo.Text.Trim <> "" AndAlso Not IsNumeric(Me.txtCKCo.Text) Then
            MsgBox(modError.TK_ERR_CKCO_INPUT_WRONG, MsgBoxStyle.Information)
            txtCKCo.Focus()
            Return False
        End If
        Return True
    End Function

    Private Function checkExist(ByVal MaTK As String, Optional ByVal oriMaTK As String = "") As Boolean
        Dim strSql As String = "Select count(*) from CTTaiKhoan where MaTK = @MaTK and Thang = @Thang and Nam = @Nam "
        Dim myConn As New SqlConnection(modKetNoi.strConnectionString)
        Dim myComm As New SqlCommand
        Dim result As Boolean = False
        Try
            myConn.Open()
            If oriMaTK <> "" Then
                strSql &= " and MaTK <> @OrigMaTK"
                myComm.Parameters.AddWithValue("@OrigMaTK", oriMaTK)
            End If
            myComm.CommandText = strSql
            myComm.Parameters.AddWithValue("@MaTK", MaTK)
            myComm.Parameters.AddWithValue("@Thang", modKetNoi.aryConfig("Thang"))
            myComm.Parameters.AddWithValue("@Nam", modKetNoi.aryConfig("Nam"))
            myComm.Connection = myConn
            If myComm.ExecuteScalar() > 0 Then
                result = True
            End If
        Catch ex As Exception

        End Try
        myConn.Close()
        myConn.Dispose()
        Return result
    End Function

    Private Function checkExistTK(ByVal MaTK As String) As Boolean
        Dim strSql As String = "Select count(*) from TaiKhoan where MaTK = @MaTK"
        Dim myConn As New SqlConnection(modKetNoi.strConnectionString)
        Dim myComm As New SqlCommand
        Dim result As Boolean = False
        Try
            myConn.Open()
            myComm.CommandText = strSql
            myComm.Parameters.AddWithValue("@MaTK", MaTK)
            myComm.Connection = myConn
            If myComm.ExecuteScalar() > 0 Then
                result = True
            End If
        Catch ex As Exception

        End Try
        myConn.Close()
        myConn.Dispose()
        Return result
    End Function

    Function getEmptyValue(ByVal value As String) As String
        If value.Trim = "" Then
            Return "0"
        End If
        Return value.Trim
    End Function
End Class